/*****************************************************************
*
*	Netcare Agent Project
*		Copyright(C) 2010, Wicresoft EKA. All Rights Reserved.
*
*	Authors:
*		Evans Liao (evansl@wicresoft.com)
*
*	Part of the product code depends on gSOAP under GPL license
*		Copyright(C) 2000-2010, Robert van Engelen, Genivia Inc. All Rights Reserved.
*
*****************************************************************/

#ifdef WIN32
// Windows
#include <windows.h>
#include <tlhelp32.h>
#else
// Linux
#endif
#include "ns.h"
#include <stdio.h>
#include "MemoryRelease.h"
NETCARE_AGENT;

//USING_STD;
CMemoryRelease::CMemoryRelease(void)
{}
CMemoryRelease::~CMemoryRelease(void)
{}


bool CMemoryRelease::Release(void)
{
#ifdef WIN32
	// CODE FROM URL
	// http://hi.baidu.com/lu4nx/blog/item/bc63dc1d5517048787d6b6b8.html

	PROCESSENTRY32 pentry = {sizeof(pentry)};              
	HANDLE hPSnap =::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); 
	BOOL bMore = ::Process32First(hPSnap,&pentry);     
	while(bMore)
	{
		HANDLE hProcess = ::OpenProcess(PROCESS_SET_QUOTA,FALSE,pentry.th32ProcessID);
		if(hProcess != NULL)
		{
			::SetProcessWorkingSetSize(hProcess, -1, -1);
		//	printf("%s\n",pentry.szExeFile);
		}
		::CloseHandle(hProcess);   

		bMore = ::Process32Next(hPSnap,&pentry); 

	}

	::CloseHandle(hPSnap);
	//getchar();
	return true;
#else
	//DO NOTHING
	return true;
#endif
}



ENDNS ENDNS